﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using LanguageFeatures.Models;


namespace LanguageFeatures.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        //public ActionResult Index()
        //{
        //    return View();
        //}

        public string Index()
        {
            return "navaigate to a url to show an example";
        }

        public ViewResult AutoProperty()
        {
            //创建一个新的Product对象
            Product myProduct = new Product();

            //设置属性值 
            myProduct.Name = "发哥你好";

            //读取属性
            string productName = myProduct.Name;

            //生成视图
            return View("Result", (object)String.Format("Product name:{0}", productName));
        }

        //这是一种方法，另一种方法演示如何构造并初始化一个数组和两个泛型集合的类
        public ViewResult CreateProduct()
        {
            //创建一个新的Porduct对象
            Product myProduct = new Product();

            //设置属性值
            myProduct.ProductID = 100;
            myProduct.Name = "发哥";
            myProduct.Description = "一件很好的商品来自中国";
            myProduct.Price = 275M;
            myProduct.Category = "水上运动";

            return View("Result", (object)string.Format("类别：{0}", myProduct.Category));
        }
        public ViewResult CreateCollection()
        {
            string[] stringArray = { "苹果电脑", "微软电脑", "联想电脑" };
            List<int> intList = new List<int> { 10, 20, 30, 40 };
            Dictionary<string, int> myDict = new Dictionary<string, int>
            {
                {"apple",10 },{"orange",20},{"plum",30}
            };
            return View("Result", (object)stringArray[1]);
            //return View("Result", (object)string.Format("intList[2]")); //错的
        }

        public ViewResult UseExtension() {
            //创建并填充ShoppingCart
            ShoppingCart cart = new ShoppingCart
            {
                Products = new List<Product>
                {
                    new Product{Name="发哥",Price=275M},
                    new Product{Name="周新华",Price=393M},
                    new Product{Name="赵无极",Price=33M},
                    new Product{Name="胡小明",Price=983M}
                }
            };

            //求购物车中产品总价
            decimal cartTotal = cart.TotalPrices();

            return View("Result", (object)string.Format("总价：{0:c}", cartTotal));
        }

        public ViewResult UseExtensionEnumberable()
        {
            IEnumerable<Product> products = new ShoppingCart
            {
                Products = new List<Product>
                {
                    new Product { Name="发哥",Price=275M},
                    new Product { Name="周新华",Price=2735M},
                    new Product { Name="易子房",Price=274M},
                    new Product { Name="胡小明",Price=33M}
                }
            };
        //创建并填充一个Product对象的数组
        Product[] productArray =
        {
                    new Product { Name="发哥",Price=275M},
                    new Product { Name="周新华",Price=2735M},
                    new Product { Name="易子房",Price=274M},
                    new Product { Name="胡小明",Price=333M}
        };

        //获取购物车产品的总价
        decimal cartTotal = products.TotalPrices();
        decimal arrayTotal = products.TotalPrices();

        return View("Result",(object)string.Format("一号购物物车cart total:{0}，二号购物车Array totala:{1}",cartTotal,arrayTotal));
        //return View("Result", (object)string.Format()
      }
        /// <summary>
        /// 创建过滤扩展方法，访问路径：https://localhost:44315/Home/UseFilterExtensionMethod
        /// </summary>
        /// <returns></returns>
        public ViewResult UseFilterExtensionMethod()
        {
            IEnumerable<Product> products = new ShoppingCart
            {
                Products = new List<Product>
                {
                    new Product {Name="发哥",Category="水上运动",Price=275M},
                    new Product {Name="车超辉",Category="水上运动",Price=175M},
                    new Product {Name="钱龙",Category="英式足球",Price=66M},
                    new Product {Name="胡强",Category="英式足球",Price=34M},
                }
            };
            decimal total = 0;
            foreach (Product prod in products.FilterByCategory("英式足球"))
            {
                total += prod.Price;
            }

            return View("Result", (object)string.Format("英式足球总价:{0}", total));
        }
        /// <summary>
        /// 使用匿名类型，访问路径：https://localhost:44315/Home/CreateAnonArray
        /// </summary>
        /// <returns></returns>
        public ViewResult CreateAnonArray()
        {
            var oddsAndEnds = new[]
            {
                new{Name="MVC",Category = "Pattern"},
                new{Name="Hat",Category = "Clothing"},
                new{Name="Apple",Category = "Fruit"},
                new{Name="Zoomla",Category = "发哥你好"}
            };
            StringBuilder result = new StringBuilder();
            foreach (var item in oddsAndEnds)
            {
                result.Append("<li>枚举出来</li>"+item.Name).Append("");
            }
            return View("Result", (object)result.ToString());
        }

        /// <summary>
        /// 基本经典方法，使用linQ集成语言查询,访问路径：https://localhost:44315/Home/Findproducts
        /// 这样的方法类似sql查询
        /// </summary>
        /// <returns></returns>

        public ViewResult Findproducts()
        {
            Product[] products =
            {
                new Product{Name="发哥",Category="婺源人",Price=275M},
                new Product{Name="胡进友",Category="婺源人",Price=295M},
                new Product{Name="汪华清",Category="婺源人",Price=136.1M},
                new Product{Name="肖强",Category="宜春人",Price=625M},
                new Product{Name="海林",Category="宜春人",Price=44.62M}
            };

            var foundProducts = from Math in products
                                orderby Math.Price descending  //ascending 是顺序
                                select new { Math.Name, Math.Price };

            //创建结果
            int count = 0;
            StringBuilder result = new StringBuilder();
            foreach (var p in foundProducts)
            {
                result.AppendFormat("最高三个price:{0}", p.Price);
                if (++count == 3) { break; }
            }
            return View("Result", (object)result.ToString());
        }
        /// <summary>
        /// 基于点符号方法，使用linQ集成语言查询,访问路径：https://localhost:44315/Home/Findproducts2
        /// 这样的方法类似sql查询，点符号法更加简洁
        /// </summary>
        /// <returns></returns>

        public ViewResult Findproducts2()
        {
            Product[] products2 =
            {
                new Product{Name="发哥",Category="婺源人",Price=369M},
                new Product{Name="胡进友",Category="婺源人",Price=95M},
                new Product{Name="汪华清",Category="婺源人",Price=4.81M},
                new Product{Name="肖强",Category="宜春人",Price=3.15M},
                new Product{Name="海林",Category="宜春人",Price=99.93255M}
            };

            var fountProducts2 = products2.OrderByDescending(e=>e.Price)
                .Take(3)
                .Select(e=> new { e.Name, e.Price });


            //创建结果
            StringBuilder result = new StringBuilder();
            foreach (var p in fountProducts2)
            {
                result.AppendFormat("最高三个Price:{0}", p.Price);
            }
            return View("Result", (object)result.ToString());
        }
        /// <summary>
        /// 基于点符号方法，使用linQ集成语言查询,访问路径：https://localhost:44315/Home/Findproducts3
        /// 这样的方法类似sql查询
        /// 本例增加了延迟查询，即Deferred
        /// </summary>
        /// <returns></returns>

        public ViewResult Findproducts3()
        {
            Product[] products3 =
            {
                new Product{Name="发哥",Category="婺源人",Price=369M},
                new Product{Name="胡进友",Category="婺源人",Price=95M},
                new Product{Name="汪华清",Category="婺源人",Price=4.81M},
                new Product{Name="肖强",Category="宜春人",Price=3.15M},
                new Product{Name="海林",Category="宜春人",Price=99.93255M}
            };

            var fountProducts3 = products3.OrderByDescending(e=>e.Price)
                .Take(3)
                .Select(e=> new { e.Name, e.Price });

            products3[2] = new Product { Name = "赵云", Price = 888.23M }; //改写第二2个，注意从0开始计数

            //创建结果
            StringBuilder result = new StringBuilder();
            foreach (var p in fountProducts3)
            {
                result.AppendFormat("最高三个Price:{0}", p.Price);
            }
            return View("Result", (object)result.ToString());
        }
        /// <summary>
        /// 基于点符号方法，使用linQ集成语言查询,访问路径：https://localhost:44315/Home/SumProducts
        /// 这样的方法类似sql查询
        /// 本例增加了延迟查询，即Deferred
        /// </summary>
        /// <returns></returns>

        public ViewResult SumProducts()
        {
            Product[] products3 =
            {
                new Product{Name="发哥",Category="婺源人",Price=369M},
                new Product{Name="胡进友",Category="婺源人",Price=95M},
                new Product{Name="汪华清",Category="婺源人",Price=4.81M},
                new Product{Name="肖强",Category="宜春人",Price=3.15M},
                new Product{Name="海林",Category="宜春人",Price=99.93255M}
            };

            var results = products3.Sum(e => e.Price);

            products3[2] = new Product { Name = "赵云", Price = 677.14M }; //改写第二2个，注意从0开始计数,但由于用了sum方法，其为非延迟方法，故不会包括在其中，作了忽略处理 

            //创建结果
            return View("Result", (object)String.Format("总合sum:{0}", results));
        }



    }
}